import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList; 
import oracle.jdbc.pool.OracleDataSource;

public class getHouses extends HttpServlet
{
	public static dbapi conn;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<a href=\"./index.html\">home</a><br><br>");
		conn = new dbapi();
		conn.connect();
		if(!conn.connected)
		{
			out.println("SQL ERROR CANNOT CONNECT TO DATABASE!<br>");
			out.println(conn.msg + "<br>");
			return;
		}
		int ct = 0;
		
		ct = conn.getHomeCount();
		if(ct == -1)
		{
			out.println("No houses in database. <br>"+conn.msg);
			return;
		}
		else
			out.println(ct+" houses in database.<br>");

		ArrayList<home> houseList = conn.getAllHomes();
		
		if(houseList == null)
		{
			out.println("Error: null resultset from database<br>"+conn.msg);
			return;
		}
		else
		{
			out.println("<table border=1 width=100%><tr>"+
				    "<th width=25%>Address</th>"+
				    "<th width=20%>City</th>"+
				    "<th width=15%>State</th>"+
				    "<th width=10%>Zipcode</th>"+
				    "<th width=30%></th>"
				     );
			for(int i=0;i<houseList.size();i++)
			{
				out.println("<tr>");
				out.println("<td>"+houseList.get(i).address+"</td>");
				out.println("<td>"+houseList.get(i).city+"</td>");
				out.println("<td>"+houseList.get(i).state+"</td>");
				out.println("<td>"+Integer.toString(houseList.get(i).zipcode)+"</td>");
				out.println("<td><a href=\"houseDetails?id="+Integer.toString(houseList.get(i).id)+"\">View House</a><br><a href=\"addAppliance?id="+Integer.toString(houseList.get(i).id)+"\">Add Appliance</a></td>");
				out.println("</tr>");
			}
			out.println("</table>");
		}
	}
}
